package com.example.zhinongbao.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhinongbao.R;
import com.example.zhinongbao.adapter.CarAdapter;
import com.example.zhinongbao.bean.Good;
import com.example.zhinongbao.utils.Constant;
import com.example.zhinongbao.utils.FragmentUtils;

import java.util.List;

/**
 * 购物车页面
 */
public class CarFragment extends Fragment {
    private CarAdapter carAdapter;
    private TextView text_total;
    private List<Good> car;
    private double total_price;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_car, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        car = Constant.currentCar;
        total_price = 0;
        initView();
    }

    private void initView() {
        carAdapter = new CarAdapter(car);
        text_total = getView().findViewById(R.id.text_total);
        RecyclerView car_recycle_view = getView().findViewById(R.id.car_recycle_view);
        LinearLayoutManager carLayout = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        car_recycle_view.setLayoutManager(carLayout);
        car_recycle_view.setAdapter(carAdapter);
        getView().findViewById(R.id.image_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentUtils.getInstance().switchFragment(R.id.nav_mine);
            }
        });
        getView().findViewById(R.id.btn_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 全选
                carAdapter.setAllSelect();
                carAdapter.setSetOnItemCheck(null);
                total_price = 0;
                for (Good good : car) {
                    Double price = Double.parseDouble(good.getPrice().replace("¥ ", ""));
                    total_price += price;
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            text_total.setText("¥ " + total_price);
                        }
                    });
                }
            }
        });

        carAdapter.setSetOnItemCheck(new setOnItemCheck() {
            @Override
            public void onCheckPosition(int position) {
                Double price = Double.parseDouble(car.get(position).getPrice().replace("¥ ", ""));
                total_price += price;
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        text_total.setText("¥ " + total_price);
                    }
                });
            }

            @Override
            public void onDisCheckPosition(int position) {
                Double price = Double.parseDouble(car.get(position).getPrice().replace("¥ ", ""));
                total_price -= price;
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        text_total.setText("¥ " + total_price);
                    }
                });
            }
        });
    }

    public interface setOnItemCheck {
        void onCheckPosition(int position);
        void onDisCheckPosition(int position);
    }

}
